package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

import control.Chatterbot;

public class guiChatBot {

	Chatterbot bot;
	private JFrame frameC;
	private JTextField textSend;

	private JMenuBar menuBar;

	private javax.swing.JTextPane textPane;
	private StyledDocument doc;

	private String sentence = "";

	public guiChatBot() {
		bot = new Chatterbot();

		frameC = new JFrame();
		frameC.setResizable(false);
		frameC.getContentPane().setBackground(new Color(20, 190, 180));
		frameC.setBounds(100, 100, 559, 462);
		frameC.setVisible(true);
		frameC.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		menuBar = new JMenuBar();
		menuBar.setBorderPainted(false);
		menuBar.setBackground(new Color(20, 190, 180));
		frameC.setJMenuBar(menuBar);

		textPane = new javax.swing.JTextPane();
		textPane.setBounds(10, 11, 414, 305);
		textPane.setForeground(Color.BLUE);
		doc = (StyledDocument) textPane.getDocument();
		String fontFace = "Tahoma";
		int fontStyle = (Font.BOLD);
		int fontSize = 12;
		Font font = new Font(fontFace, fontStyle, fontSize);
		textPane.setFont(font);

		JScrollPane scrll = new JScrollPane();
		scrll.setBounds(10, 11, 414, 305);
		scrll.setViewportView(textPane);
		frameC.getContentPane().add(scrll);

		textSend = new JTextField();
		textSend.setBackground(Color.WHITE);
		textSend.setBounds(10, 358, 414, 34);
		frameC.getContentPane().add(textSend);
		textSend.setColumns(10);

		JButton Send = new JButton("Send");
		Send.setForeground(new Color(20, 190, 180));
		Send.setFont(new Font("Tahoma", Font.BOLD, 12));
		Send.setBounds(441, 358, 102, 34);
		frameC.getContentPane().add(Send);

		Send.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent arg0) {
				sentence = textSend.getText();
				display(bot.getName() + " : " + sentence);
				textSend.setText("");
				try {
					bot.process(sentence);
					display(bot.getAnswer());
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		});
		
		this.frameC.repaint();

	}

	public String getSentence() {
		return sentence;
	}

	public void display(String s) {
		try {
			doc.insertString(doc.getLength(), "\n" + s, null);
		} catch (BadLocationException badLocationException) {
			System.err.println("Oops");
		}
	}

}
